import {
  getUserInfo,
  getMessageDetail,
  getMessageInfo,
  getChatList, unLockFunc
} from '../../api/member.js';
import Util from '../../utils/util.js';
import {getAdviceReply} from "../../api/api";

// const websocket = require('../../utils/websocket.js');

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: false, // 是否加载中
    loadend: false, // 是否加载完毕
    loadTitle: '加载更多内容', // 提示语
    lists: [],
    page: 1,
    et_show: true,

    accountDialog: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    const suid = options.suid || ''
    this.setData({
      send_uid: suid,
      msg_id: options.msg_id || "",
      userInfo: app.globalData.userInfo || [],
      bottom: app.globalData.footerPadding
    })
    wx.setStorageSync('is_refresh', 1)

    const that = this;
    if (Util.isEmpty(this.data.send_uid)) {  //加载系统消息
      this.setData({
        loadend: false,
        loading: false,
        page: 1,
        lists: []
      });

      this.getLists()
    } else {
      getMessageInfo(that.data.msg_id).then(res => {
        let member = res.data.member
        if (member.current_status && member.current_status.indexOf("读") != -1) {
          member.is_student = 1;
        }

        let userInfo = res.data.current_member || that.data.userInfo || null
        if ((userInfo && userInfo.is_vip) || (member.unlock_funcs && member.unlock_funcs.indexOf('ablum')) != -1) {
          member.unlock_ablum = 1;
        }

        that.setData({
          message: res.data.message || '',
          send_member: member || null,
          userInfo: res.data.current_member || null,
          unlock_type: res.data.unlock_type || null
        })

      }).catch(errmsg => {
        app.Tips({title: errmsg, hideMask: true})
      })
    }

    this.onLoadTabbar();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  goToPage(e) {
    const url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: url
    })
  },

  getLists() {
    const that = this
    let lists = that.data.lists || []

    if (that.data.loadend || that.data.loading) {
      wx.stopPullDownRefresh()
      return;
    }
    if (!Util.isEmpty(this.data.send_uid)) {
      wx.stopPullDownRefresh()
      return false;
    }

    that.setData({loading: true, loadTitle: '加载更多内容'});

    let filter = that.data.filter || {}
    filter['page_num'] = that.data.page;
    filter['df_uid'] = that.data.df_uid || '';

    getMessageDetail(filter).then(res => {
      const list = res.data.items || []
      const loadend = res.data.last_page <= that.data.page;

      lists = app.SplitArray(list, lists);
      if (this.data.send_uid) {
        lists = lists.reverse();
      }

      that.setData({
        lists: lists,
        loadend: loadend,
        loading: false,
        loadTitle: loadend ? '已全部加载' : '正在加载...',
        total_unread: res.data.total_unread || 0,
        page: that.data.page + 1,
        et_show: lists.length > 0
      });

      wx.stopPullDownRefresh()
    }).catch(errmsg => {
      wx.stopPullDownRefresh()
      app.Tips({title: errmsg})
    })
  },

  bindUnlock(e) { //显示解锁弹窗
    this.setData({
      memberComboDialog: true
    })
  },

  dialogCancel(e) {
    const that = this,
      dialog_data = e.detail;
    // && dialog_data.field == 'filter'
    if (!Util.isEmpty(dialog_data.field)) {
      this.setData({
        memberComboDialog: true
      })
      return
    }
  },

  confirmMemberComboDialog(e) {
    if (!Util.isEmpty(e.detail.is_vip) && e.detail.is_vip == 1 && !Util.isEmpty(this.data.member)) {
      let member = this.data.member
      member.is_vip = 1;
      this.setData({
        member: member
      })
      return
    }
  },

  //弹窗回调
  dialogConfirm(e) {
    const that = this,
      dialog_data = e.detail;

    let send_member = that.data.send_member
    if (!dialog_data.type) return;
    if (dialog_data.type == 'account') { //账户余额
      wx.navigateTo({
        url: '/pages/account/refund'
      })
      return;
    }

    const isAll = that.data.stepby.is_all
    if (dialog_data.type == 'profile' && isAll == 0) { //资料不完善跳转
      //is_all=1资料已完善；is_all=0资料未完善；
      if (isAll == 0) {
        const type = that.data.stepby.type[0] || '';

        if (type == 12) {
          const url = "/pages/member/info/edit_avatar?type=12";
          wx.navigateTo({
            url: url,
          })
        } else if (type == 99) {//请验证手机号码
          app.Tips("../public/verify_mobile")
        } else {
          wx.navigateTo({
            url: '../member/info/edit?type=' + type
          })
        }
      }
      return
    }

    if (dialog_data.type == 12) {
      that.addElected(e, 0)
    }

  },

  previewImage(e) {
    let src = e.currentTarget.dataset.src; //需要保存的图片地址
    if (!src)
      return app.Tips({title: "二维码不存在"})

    const urls = []
    urls.push(src);
    wx.previewImage({
      current: src,
      urls: urls
    });
  },

  copyWeixin: function (event) {
    const weixin = event.currentTarget.dataset.weixin //接收wxml传过来的数据

    wx.setClipboardData({
      data: weixin,
      success: function (res) {
        return app.Tips({title: "复制成功"})
      }
    })
  },

  onLoadTabbar: function () {//底部tabbar返回数据
    const that = this;
    Util.checkIphone().then(res => {//检查是否是iphone x/11等
      that.setData({
        isIphoneX: res,
      })
    })
    wx.createSelectorQuery().in(that).select('#weui-footer')
      .boundingClientRect(function (rect) {
        if (!rect) return false;

        that.setData({
          bottom: parseInt(rect.height) + 15
        })
      }).exec()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    //关闭socket
    // websocket.closeConnect();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getLists();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})